﻿//#define SET_US

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ENG.TeachersDiary
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);

#if NEVER_DO_THIS
      Properties.Settings.Default.DBFile = 
        @"C:\Documents and Settings\Marek Vajgl\Data aplikací\Marek Vajgl\Teachers diary\MisaDiaryDb.sdf";
#endif

#if SET_US
      System.Threading.Thread.CurrentThread.CurrentUICulture =
        new System.Globalization.CultureInfo("en-US");
#endif

      CreateProgress();
      
      string dbFile = Properties.Settings.Default.DBFile;
      if (string.IsNullOrEmpty(dbFile))
      {
        dbFile = System.IO.Path.Combine(Common.UserAppDataPath, "DiaryDb.sdf");
        if (!System.IO.File.Exists(dbFile))
        {
          Common.CreateNewDB(dbFile);
          MessageBox.Show(Properties.Resources.FirstStartDbCreated);
        }

        Properties.Settings.Default.DBFile = dbFile;
        Properties.Settings.Default.Save();
      }
      else
      {
        try
        {
          var dbc = Common.DBConn;
          var test = dbc.SchoolYears.ToList();
        }
        catch (Exception ex)
        {
          MessageBox.Show(Properties.Resources.DBOpenFailed + " " + Properties.Settings.Default.DBFile + ". " +
           Properties.Resources.SelectDBInOptions  + " " + ex.Message,
            Properties.Resources.ErrorDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

          FrmOptions fo = new FrmOptions();

          fo.ShowDialog();

          Application.Restart();
          
        }

        if (Properties.Settings.Default.LastBackupDate.AddMonths(1) < DateTime.Now)
        {
          var ret = MessageBox.Show(Properties.Resources.BackupReccomendation,
            Application.ProductName,
             MessageBoxButtons.YesNo, MessageBoxIcon.Question);
          if (ret == DialogResult.Yes)
          {
            Common.DoBackup();
          }
        }
      }

      Common.DoQuickBackup();

      FrmIntro f = new FrmIntro();

      f.ShowDialog();

      if (f.DialogResult != DialogResult.OK)
        return;

      ShowProgress(Properties.Resources.LoadingApplication);

      Common.CurrentClassID = f.SelectedClass.ClassID;
      Common.CurrentSubjectID = f.SelectedSubject.SubjectID;

      FrmMain frmMain = new FrmMain();

      frmMain.Init();

      Application.Run(frmMain);
    }

    //private static System.Threading.Thread bgThread = null;
    private static FrmProgress frmProgress = null;

    public static void ShowProgress()
    {
      ShowProgress("Please wait...");
    }
    public static void ShowProgress(string infoText)
    {
      frmProgress.TopMost = true;
      frmProgress.SetInfoText(infoText);
      frmProgress.Show();
      Application.DoEvents();      
    }

    public static void UpdateProgress(string infoText)
    {
      frmProgress.SetInfoText(infoText);
    }

    public static void HideProgress()
    {
      frmProgress.Hide();
    }

    public static void CreateProgress()
    {
      frmProgress = new FrmProgress();
    }


  }
}
